<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model;

/**
 * @property int $id ID
 * @property string $project_id 方案ID
 * @property int $userid 用户ID
 * @property int $sport_id 运动类型
 * @property string $game_id 赛事ID
 * @property string $match_id 比赛ID
 * @property string $match_odds 比赛指数
 * @property string $title 标题
 * @property string $detail 方案详情
 * @property string $introduce 解读
 * @property int $expert_win_rate 专家近期胜率s
 * @property int $expert_payback_rate 专家近期回报率
 * @property int $status 状态 1-正在售卖 2-售卖结束 3-已公布
 * @property int $coins 查看金币
 * @property int $sales 售出数量
 * @property int $total_amount 总销售金币
 * @property int $hit_status 命中状态 1-命中 2-未中 3-走
 * @property int $hits 点击量
 * @property int $play_type_code 玩法ID
 * @property string $play_type_name 玩法名称
 * @property string $suggest_result_code 推荐比赛结果
 * @property string $real_result_code 实际比赛结果
 * @property int $add_time 发布时间
 * @property-read null|UserModelExpert $expert 
 * @property-read null|User $user 
 * @property-read null|Play $match 
 */
class Project extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'project';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'project_id', 'userid', 'sport_id', 'game_id', 'match_id', 'match_odds', 'title', 'detail', 'introduce', 'expert_win_rate', 'expert_payback_rate', 'status', 'coins', 'sales', 'total_amount', 'hit_status', 'hits', 'play_type_code', 'play_type_name', 'suggest_result_code', 'real_result_code', 'add_time'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'userid' => 'integer', 'sport_id' => 'integer', 'expert_win_rate' => 'integer', 'expert_payback_rate' => 'integer', 'status' => 'integer', 'coins' => 'integer', 'sales' => 'integer', 'total_amount' => 'integer', 'hit_status' => 'integer', 'hits' => 'integer', 'play_type_code' => 'integer', 'add_time' => 'integer'];

    public function expert()
    {
        return $this->hasOne(UserModelExpert::class, 'userid', 'userid');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'userid')->select(['id', 'username', 'nickname', 'avatar']);
    }

    public function match()
    {
        return $this->hasOne(Play::class, 'match_id', 'match_id');
    }

    public static function createId(): string
    {
        $id = strtolower(create_randomstr(15));
        if (self::query()->where('project_id', $id)->exists()) {
            return self::createId();
        }
        return $id;
    }
}
